package com.cpi.cornpp.exchangerateversion.converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.exchangerateversion.dto.ExchangeRateVersionDTO;
import com.cpi.cornpp.exchangerateversion.exception.ExchangeRateVersionException;
import com.cpi.cornpp.exchangerateversion.view.ExchangeRateGroupView;
import com.cpi.cornpp.exchangerateversion.view.ExchangeRateVersionView;
import com.cs.common.utils.NumberUtil;

public class ExchangeRateVersionConverter {

	public List viewGroups(List ExchangeRateVersionView){
		ArrayList groups = null;
		HashMap aux = null;
		Iterator iterator = null;
		groups = new ArrayList();
		aux = new HashMap();
		ExchangeRateVersionView view = null;
		
		iterator = ExchangeRateVersionView.iterator();
		while( iterator.hasNext() ){
			view = (ExchangeRateVersionView)iterator.next();
			if(!aux.containsKey(view.getCurrency())){
				ExchangeRateGroupView group = new ExchangeRateGroupView();
				group.setCurrency(view.getCurrency());
				group.getExchangeRateVersionViews().add(view);
				aux.put(view.getCurrency(), group);
				groups.add(group);
			}else{
				ExchangeRateGroupView group = (ExchangeRateGroupView)aux.get(view.getCurrency());
				group.getExchangeRateVersionViews().add(view);
			}
		}
		
		
		return groups;
		
	}
	
	public List convertDTOsToViews(List exchangeRateVersionDTOs)throws ExchangeRateVersionException{
		List exchangeRateVersionViews = null;
		Iterator iterator = null;
		ExchangeRateVersionDTO exchangeRateVersionDTO = null; 
		ExchangeRateVersionView exchangeRateVersionView = null;
		try{
			iterator = exchangeRateVersionDTOs.iterator();
			exchangeRateVersionViews = new ArrayList(0);
			while( iterator.hasNext() ){
				exchangeRateVersionDTO = (ExchangeRateVersionDTO)iterator.next();
				exchangeRateVersionView = convertDTOToView(exchangeRateVersionDTO);
				exchangeRateVersionViews.add(exchangeRateVersionView);
			}
		}catch(ExchangeRateVersionException exchangeRateVersionException){
			throw exchangeRateVersionException;
		}catch( Exception exception ){
			ExchangeRateVersionException exchangeRateVersionException=null;
			exception.printStackTrace(System.out);
			exchangeRateVersionException= new ExchangeRateVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw exchangeRateVersionException;
		}
		return exchangeRateVersionViews;
	}

	
	public ExchangeRateVersionView convertDTOToView(ExchangeRateVersionDTO exchangeRateVersionDTO) throws ExchangeRateVersionException{
		ExchangeRateVersionView exchangeRateVersionView = null;
		String formatedQuantity = null;
		try{
			if( exchangeRateVersionDTO != null ){
				exchangeRateVersionView = new ExchangeRateVersionView();
				exchangeRateVersionView.setVersionId(exchangeRateVersionDTO.getMonthPeriodVersionId());
				exchangeRateVersionView.setCurrency(exchangeRateVersionDTO.getCurrency());
				formatedQuantity = NumberUtil.convertPrice(exchangeRateVersionDTO.getAmount());
				exchangeRateVersionView.setAmount(formatedQuantity);
				formatedQuantity = NumberUtil.convertPrice(exchangeRateVersionDTO.getAvgAmount());
				exchangeRateVersionView.setAvgAmount(formatedQuantity);
			}
		}catch( Exception exception ){
			ExchangeRateVersionException exchangeRateVersionException=null;
			exception.printStackTrace(System.out);
			exchangeRateVersionException= new ExchangeRateVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw exchangeRateVersionException;
		}
		return exchangeRateVersionView;
	}
	
	public ExchangeRateVersionDTO convertViewToDTO(ExchangeRateVersionView exchangeRateVersionView) throws ExchangeRateVersionException{
		ExchangeRateVersionDTO exchangeRateVersionDTO = null;		
		try{
			if( exchangeRateVersionView != null ){
				exchangeRateVersionDTO = new ExchangeRateVersionDTO();
				exchangeRateVersionDTO.setMonthPeriodVersionId(exchangeRateVersionView.getVersionId());
				exchangeRateVersionDTO.setCurrency(exchangeRateVersionView.getCurrency());
				exchangeRateVersionDTO.setAmount(NumberUtil.parseDouble(exchangeRateVersionView.getAmount()));
				exchangeRateVersionDTO.setAvgAmount(NumberUtil.parseDouble(exchangeRateVersionView.getAvgAmount()));
				
			}
		}catch( Exception exception ){
			ExchangeRateVersionException exchangeRateVersionException=null;
			exception.printStackTrace(System.out);
			exchangeRateVersionException= new ExchangeRateVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw exchangeRateVersionException;
		}
		return exchangeRateVersionDTO;
	}
	
	public List convertViewsToDTOs(List exchangeRateVersionViews)throws ExchangeRateVersionException{
		List exchangeRateVersionDTOs = null;
		Iterator iterator = null;
		ExchangeRateVersionView exchangeRateVersionView = null; 
		ExchangeRateVersionDTO exchangeRateVersionDTO= null;
		try{
			iterator = exchangeRateVersionViews.iterator();
			exchangeRateVersionDTOs = new ArrayList(0);
			while( iterator.hasNext() ){
				exchangeRateVersionView= (ExchangeRateVersionView)iterator.next();
				exchangeRateVersionDTO = convertViewToDTO(exchangeRateVersionView);
				exchangeRateVersionDTOs.add(exchangeRateVersionDTO);
			}
		}catch(ExchangeRateVersionException exchangeRateVersionException){
			throw exchangeRateVersionException;
		}catch( Exception exception ){
			ExchangeRateVersionException exchangeRateVersionException=null;
			exception.printStackTrace(System.out);
			exchangeRateVersionException= new ExchangeRateVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw exchangeRateVersionException;
		}
		return exchangeRateVersionDTOs;
	}
	
	
}